// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations;

[JsonConverter(typeof(IpPrefixBucketConverter))]
public sealed partial class IpPrefixBucket : AggregateDictionary
{
	public IpPrefixBucket(IReadOnlyDictionary<string, IAggregate> backingDictionary) : base(backingDictionary)
	{
	}

	[JsonInclude, JsonPropertyName("doc_count")]
	public long DocCount { get; init; }
	[JsonInclude, JsonPropertyName("is_ipv6")]
	public bool IsIpv6 { get; init; }
	[JsonInclude, JsonPropertyName("key")]
	public string Key { get; init; }
	[JsonInclude, JsonPropertyName("netmask")]
	public string? Netmask { get; init; }
	[JsonInclude, JsonPropertyName("prefix_length")]
	public int PrefixLength { get; init; }
}

internal sealed class IpPrefixBucketConverter : JsonConverter<IpPrefixBucket>
{
	public override IpPrefixBucket? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException($"Expected {JsonTokenType.StartObject} but read {reader.TokenType}.");
		var subAggs = new Dictionary<string, IAggregate>();// TODO - Optimise this and only create if we need it.
		long docCount = default;
		bool isIpv6 = default;
		string key = default;
		string? netmask = default;
		int prefixLength = default;
		while (reader.Read())
		{
			if (reader.TokenType == JsonTokenType.EndObject)
				break;
			if (reader.TokenType != JsonTokenType.PropertyName)
				throw new JsonException($"Expected {JsonTokenType.PropertyName} but read {reader.TokenType}.");
			var name = reader.GetString();// TODO: Future optimisation, get raw bytes span and parse based on those
			reader.Read();
			if (name.Equals("doc_count", StringComparison.Ordinal))
			{
				docCount = JsonSerializer.Deserialize<long>(ref reader, options);
				continue;
			}

			if (name.Equals("is_ipv6", StringComparison.Ordinal))
			{
				isIpv6 = JsonSerializer.Deserialize<bool>(ref reader, options);
				continue;
			}

			if (name.Equals("key", StringComparison.Ordinal))
			{
				key = JsonSerializer.Deserialize<string>(ref reader, options);
				continue;
			}

			if (name.Equals("netmask", StringComparison.Ordinal))
			{
				netmask = JsonSerializer.Deserialize<string?>(ref reader, options);
				continue;
			}

			if (name.Equals("prefix_length", StringComparison.Ordinal))
			{
				prefixLength = JsonSerializer.Deserialize<int>(ref reader, options);
				continue;
			}

			if (name.Contains("#"))
			{
				AggregateDictionaryConverter.ReadAggregate(ref reader, options, subAggs, name);
				continue;
			}

			throw new JsonException("Unknown property read from JSON.");
		}

		return new IpPrefixBucket(subAggs) { DocCount = docCount, IsIpv6 = isIpv6, Key = key, Netmask = netmask, PrefixLength = prefixLength };
	}

	public override void Write(Utf8JsonWriter writer, IpPrefixBucket value, JsonSerializerOptions options) => throw new NotImplementedException();
}